﻿using System;
using System.Globalization;

namespace Medianamik.Core
{
    public class LookupNodeProperty : NodeProperty
    {
        internal LookupNodeProperty(int? pID, string pValue, string pCultureName, int? pRevision, Node pNode, OneToManyRelation pRelation)
            : base(pID, pRelation.ForeignProperty, pValue, pCultureName, pRevision, pNode)
        {
            Relation = pRelation;

        }

        internal LookupNodeProperty(string pValue, string pCultureName, Node pNode, OneToManyRelation pRelation)
            : base(pRelation.ForeignProperty, pValue, pCultureName, pNode)
        {
            Relation = pRelation;
        }

        public OneToManyRelation Relation
        {
            get;
            private set;
        }

        public override bool IsLookup
        {
            get
            {
                return true;
            }
        }

        //private SortedList<string, Guid> _options;
        protected RelationOptionCollection GetOptions(CultureInfo pCulture)
        {
            return NodeManager.GetRelationOptions(Relation
                        , pCulture.Name, Node, MedianamikControlInfo.TreeRootNodeID);
        }

        public override IMedianamikControl GetMedianamikControl(bool pEnableClientScriptValidation,
            bool pAutoGenerateValidators, string tabName, bool pDisplayLabel, string pLabel, string pLabelLanguageExtensionText,
            string pLabelExtensionText, bool hideMultiLingualControls, bool hideNeutralLanguageIndication, bool? isRequired)
        {
            var control = base.GetMedianamikControl(pEnableClientScriptValidation,
                pAutoGenerateValidators, tabName, pDisplayLabel, pLabel, pLabelLanguageExtensionText, pLabelExtensionText,
                hideMultiLingualControls, hideNeutralLanguageIndication, isRequired);

            //Cette propriété est spécifique pour le controle OneToMany
            var editionCulture = MedianamikContext.Current.EditionCulture;
            control.OptionsDelegate = () => GetOptions(editionCulture);

            return control;
        }

        public override IMedianamikControl GetMedianamikControl(bool pEnableClientScriptValidation,
            bool pAutoGenerateValidators, string tabName, bool pDisplayLabel, string pLabel, string pLabelLanguageExtensionText,
            string pLabelExtensionText, bool hideMultiLingualControls, bool hideNeutralLanguageIndication, Guid medianamikControlId, bool? isRequired)
        {
            var control = base.GetMedianamikControl(pEnableClientScriptValidation,
                pAutoGenerateValidators, tabName, pDisplayLabel, pLabel, pLabelLanguageExtensionText, pLabelExtensionText,
                hideMultiLingualControls, hideNeutralLanguageIndication, medianamikControlId, isRequired);

            //Cette propriété est spécifique pour le controle OneToMany
            var editionCulture = MedianamikContext.Current.EditionCulture;
            control.OptionsDelegate = () => GetOptions(editionCulture);

            return control;
        }
    }
}